from code.data.Homework import Homework
from code.data.Answer import Answer
from code.data.User import User
from code.dao.user_management import search_user
from code.dao.homework_management import search_homework,add_homework,update_homework,delete_homework
from code.dao.answer_management import add_answer,search_answer_by_homeworkID,delete_answer,update_answer,search_answer

class HomeworkBackend:
    @staticmethod
    def homework_init_display_for_student(userin:User):
        ret = 0
        homework_ret = 0
        if userin.userID is None:
            ret = 1001
        else:
            user = search_user(userin, "userID")[0]
            if user.type == "Student":
                answer_back = search_answer(Answer(userID= user.userID), "userID")              #学生依据answer表查询自己已提交未提交的作业 status
                if answer_back is None:
                    ret = 1026
                else:
                    ret =answer_back
                    for items in answer_back:
                        homework_ret = search_homework(Homework(homeworkID= items.homeworkID),"homeworkID")
            else:
                ret = 1005
        return ret, homework_ret

    @staticmethod
    def homework_init_display_for_teacher(userin:User):
        ret = 0
        if userin.userID is None:
            ret = 1001
        else:
            user = search_user(userin, "userID")[0]
            if user.type =="Teacher" or user.type =="admin":
                ret = search_homework(Homework(),"all")
            else:
                ret = 1006
        return ret

    @staticmethod
    def homework_announce(userin:User, homework:Homework):
        ret = 0
        if userin.userID is None:
            ret = 1001
        else:
            user = search_user(userin, "userID")[0]
            if user.type == "Student":
                ret = 1002
            elif homework.content is None:
                ret = 1003
            elif homework.file_path is not None:
                if homework.file_size > 1000000000:
                    ret = 1004
            else:
                add_homework_ret = add_homework(homework)
                homework_back_list = search_homework(Homework(),"all")
                homework_back_list.sort(key=lambda homework: homework.homeworkID, reverse=True)
                homeworkID = homework_back_list[0].homeworkID
                userlist = search_user(User(),"all")
                if userlist is None:
                    if add_homework_ret is True:
                        ret = 1
                    else:
                        ret = 0
                else:
                    ret_for_all_add_answer_init_tuple = 1
                    for user in userlist:
                        add_answer_init_tuple_ret = add_answer(Answer(status=0, userID=user.userID, homeworkID= homeworkID))
                        if add_answer_init_tuple_ret is False:
                            ret_for_all_add_answer_init_tuple = 0
                    ret = ret_for_all_add_answer_init_tuple
        return ret

    @staticmethod
    def homework_delete(userin:User, homeworkin:Homework):
        ret = 0
        if userin.userID is None:
            ret = 1001
        elif homeworkin.homeworkID is None:
            ret = 1007
        else:
            user = search_user(userin, "userID")[0]
            homework = search_homework(homeworkin, "homeworkID")[0]
            if homework is None:
                ret = 1008
            elif user.userID != homework.userID:
                ret = 1009
            else:
                ret_delete_homework = delete_homework(homework, "homeworkID")
                ret_delete_answer = delete_answer(Answer(homeworkID= homework.homeworkID), "homeworkID")
                if ret_delete_answer !=1 or ret_delete_homework !=1:
                    ret = 1010
                else:
                    ret = 1
        return ret

    @staticmethod
    def homework_update(userin:User, homeworkin:Homework):
        ret = 0
        if userin.userID is None:
            ret = 1001
        elif homeworkin.homeworkID is None:
            ret = 1007
        else:
            user = search_user(userin, "userID")[0]
            homework = search_homework(homeworkin, "homeworkID")[0]
            if homework is None:
                ret = 1008
            elif user.userID != homework.userID:
                ret = 1009
            elif homeworkin.file_path is not None:
                if homeworkin.file_size > 1000000000:
                    ret = 1011
            else:
                homework_modify = Homework(homeworkID=homework.homeworkID,time=homework.time,content=homework.content,file_path=homework.file_path,file_size=homework.file_size,userID=homework.userID )
                ret1 = update_homework(homework_modify,"content")
                ret2 = update_homework(homework_modify,"file_path")
                ret3 = update_homework(homework_modify,"file_size")
                if ret1 is True or ret2 is True or ret3 is True:
                    ret = 0
                else:
                    ret =1
        return ret


